package 力扣._96不同的二叉搜索树;

import java.util.Arrays;

public class Solution {
    public int numTrees(int n) {
        //以每个节点为根有
        //dp[i]: 以i个节点能组成dp[i]种不同的BST
        //dp[i] += dp[j]*dp[i-j-1], 左子树j个，右子树i-j-1个
        int[] dp = new int[n + 3];
        dp[0] = 1;
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                dp[i] += dp[j] * dp[i - j - 1];
            }
        }
//        System.out.println(Arrays.toString(dp));
        return dp[n];
    }

    public static void main(String[] args) {
        int i = new Solution().numTrees(14);
        System.out.println(i);
    }
}
